package com.hbgc.controller;

import com.github.pagehelper.PageInfo;
import com.hbgc.common.Combox;
import com.hbgc.common.PageResult;
import com.hbgc.pojo.TbType;
import com.hbgc.service.IVideoTypeService;
import com.hbgc.utils.ConvertUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/videotype")
public class VideoTypeController {

    @Autowired
    private IVideoTypeService iVideoTypeService;

    /**
     * 保存数据
     * @param tbType
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public  PageResult save(TbType tbType)
    {
         System.out.println(tbType);
         boolean status=iVideoTypeService.saveTbType(tbType);
         if(status)
         {
          return new PageResult(0,"保存成功");
         }
         else {
         return new PageResult(1,"保存失败");
         }
    }


    /**
     * 修改数据
     * @param tbType
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public  PageResult update(TbType tbType)
    {

        boolean status=iVideoTypeService.updateTbTypeById(tbType);
        if(status)
        {
            return new PageResult(0,"修改成功");
        }
        else {
            return new PageResult(1,"修改失败");
        }
    }

    /**
     * 删除单个
     * @param typeid
     * @return
     */
    @RequestMapping("/remove/{typeid}")
    @ResponseBody
    public  PageResult remove(@PathVariable long typeid)
    {

        boolean status=iVideoTypeService.deleteTbTypeById(typeid);
        if(status)
        {
            return new PageResult(0,"删除成功");
        }
        else {
            return new PageResult(1,"删除失败");
        }
    }


    /**
     * 查询单个
     * @param typeid
     * @return
     */
    @RequestMapping("/querybyid/{typeid}")
    @ResponseBody
    public  PageResult querybyid(@PathVariable long typeid)
    {

        TbType tbType=iVideoTypeService.getTbTypeById(typeid);
        if(tbType!=null)
        {
            PageResult pageResult=new PageResult(0,"查询成功");
            List data=new ArrayList();
            data.add(tbType);
            pageResult.setData(data);
            return pageResult;
        }
        else {
            return new PageResult(1,"查询失败");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param typename
     * @return
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public PageResult queryByPage(@RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "10") int limit, @RequestParam(required = false,defaultValue = "") String typename)
    {
        PageInfo pageInfo=iVideoTypeService.queryTbTypeByPage(page,limit,typename);
        return ConvertUtils.convertData(pageInfo);
    }


    /**
     *
     * @return
     */
    @RequestMapping("/queryByPidAll/{pid}")
    @ResponseBody
    public PageResult queryByPidAll(@PathVariable long pid)
    {
        PageResult pageResult=new PageResult();
        boolean status=pid==1?true:false;
        List<TbType>tbTypeList=iVideoTypeService.queryTbTypeByPid(status);
        pageResult.setCount(tbTypeList.size());
        pageResult.setCode(0);
        pageResult.setMsg("查询成功");
        List<Combox> comboxList=new ArrayList<>();
        for(TbType tbType:tbTypeList)
        {
            Combox combox=new Combox();
            combox.setId(tbType.getTypeid());
            if(tbType.getPid()!=0) {
             combox.setTitle("<span style='margin-left:20px'>"+tbType.getTypename()+"</span>");
            }
            else
            {
                combox.setTitle(tbType.getTypename());
            }

            comboxList.add(combox);
        }
        pageResult.setData(comboxList);
        return pageResult;
    }

}
